import requests
import json
import time
from datetime import datetime

# 配置
BASE_URL = "http://localhost:8888"
BATCH_NUMBER = "001"  # 批次号

# 设备数据
devices = [
    {
        "id": 5,
        "dev_id": "12eea2bf735e4aca9b27e4bbbd982f91",
        "cpu_ser": "9aab5e01df8764c5",
        "emmc_ser": "0x39d5ce5c",
        "mac_add": "60:48:9c:41:d6:b8",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759112744.1034112
    },
    {
        "id": 6,
        "dev_id": "9b359931b9797b2bdb75064c8653d378",
        "cpu_ser": "611a7f93accbcd5b",
        "emmc_ser": "0x39d5cf50",
        "mac_add": "60:48:9c:41:c5:42",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759129402.1139073
    },
    {
        "id": 7,
        "dev_id": "03b109e2a8fbeffdc7e235e08db3988e",
        "cpu_ser": "961a5bb9e914912e",
        "emmc_ser": "0x3ad5c006",
        "mac_add": "60:48:9c:41:c5:26",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759139422.581407
    },
    {
        "id": 8,
        "dev_id": "d4aebedf070d00b6d37204615379a613",
        "cpu_ser": "c5d8a8315c54e102",
        "emmc_ser": "0x39d5cfc0",
        "mac_add": "60:48:9c:41:d6:0c",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759139590.7974036
    },
    {
        "id": 9,
        "dev_id": "5f045bdee1119612e2ecf193a5514fe6",
        "cpu_ser": "f29153ddbc81081c",
        "emmc_ser": "0x39d5cfbd",
        "mac_add": "60:48:9c:41:d6:36",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759139708.8030648
    },
    {
        "id": 10,
        "dev_id": "291d4587eabb30be244a09e3fc945e8c",
        "cpu_ser": "d188c2b642c5ac77",
        "emmc_ser": "0x3ad5bfe5",
        "mac_add": "60:48:9c:41:c6:48",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140028.8244572
    },
    {
        "id": 11,
        "dev_id": "30be5c92367f6a43660a2c2423d5acc3",
        "cpu_ser": "81087ed9641c9cdb",
        "emmc_ser": "0x39d5cf07",
        "mac_add": "60:48:9c:41:c5:20",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140260.1453989
    },
    {
        "id": 12,
        "dev_id": "ccd24cccbc7597f792acd71b6972bac5",
        "cpu_ser": "1c1c667f8d7f39ee",
        "emmc_ser": "0x3a156f12",
        "mac_add": "60:48:9c:41:d6:32",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140345.1758482
    },
    {
        "id": 13,
        "dev_id": "dbc5f8465b4ddce50250dada9d0fd5f9",
        "cpu_ser": "38a51e50258859a6",
        "emmc_ser": "0x3a956eaa",
        "mac_add": "60:48:9c:41:d6:88",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140583.7634358
    },
    {
        "id": 14,
        "dev_id": "ee3a42ab195a190f671d76be0c7dccef",
        "cpu_ser": "764ce629d8eaa0dc",
        "emmc_ser": "0x3a956f81",
        "mac_add": "60:48:9c:41:c6:1a",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140722.3656988
    },
    {
        "id": 15,
        "dev_id": "2b3d69a2197f4902c219a393423b76f7",
        "cpu_ser": "090dffb6ccebfc93",
        "emmc_ser": "0x39d5cf3f",
        "mac_add": "60:48:9c:41:d5:ae",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140752.0304687
    },
    {
        "id": 16,
        "dev_id": "6775fafe44ccf54dfadedc924efd0d5e",
        "cpu_ser": "18012f63641c9cdb",
        "emmc_ser": "",
        "mac_add": "60:48:9c:41:d6:42",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140802.2876263
    },
    {
        "id": 17,
        "dev_id": "0f005d143e4a4cbfc542a4a4d5d98de3",
        "cpu_ser": "e805fe73dbccfdcd",
        "emmc_ser": "0x3a956e03",
        "mac_add": "60:48:9c:41:c5:30",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140853.6365466
    },
    {
        "id": 18,
        "dev_id": "b86072ed053d47976a800fc5a9afaf33",
        "cpu_ser": "82fba752bc81081c",
        "emmc_ser": "0x3a55f430",
        "mac_add": "60:48:9c:41:d6:46",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140864.1255093
    },
    {
        "id": 19,
        "dev_id": "842abf8cd49c8edb98e5a64881cde41a",
        "cpu_ser": "5bfe9ed841e49ca2",
        "emmc_ser": "0x3ad5be82",
        "mac_add": "60:48:9c:41:d6:a8",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759140989.596063
    },
    {
        "id": 20,
        "dev_id": "9b3dbd465468b8ba0c93349156f3b0c6",
        "cpu_ser": "f21207538d7f39ee",
        "emmc_ser": "0x3994319c",
        "mac_add": "60:48:9c:41:c5:40",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141180.7191865
    },
    {
        "id": 21,
        "dev_id": "ca2c8a206bb97397cf5a98a22fa14bf1",
        "cpu_ser": "762f1203bc81081c",
        "emmc_ser": "0x3a956fb1",
        "mac_add": "60:48:9c:41:c6:38",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141256.3954806
    },
    {
        "id": 22,
        "dev_id": "fa631d3647ea2c1ddfd7bd336fcea3b9",
        "cpu_ser": "bb1faab4df8764c5",
        "emmc_ser": "",
        "mac_add": "60:48:9c:41:c6:58",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141325.0810287
    },
    {
        "id": 23,
        "dev_id": "227972b250eb0b65903d71f4d7e73306",
        "cpu_ser": "20c725e13c74d0ca",
        "emmc_ser": "0x3a956f80",
        "mac_add": "60:48:9c:41:c6:8a",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141403.5507615
    },
    {
        "id": 24,
        "dev_id": "28fbad28c397a8c275fc0af2f1b6f93a",
        "cpu_ser": "67a09658258859a6",
        "emmc_ser": "0x3a956eaf",
        "mac_add": "60:48:9c:41:d6:28",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141467.3836892
    },
    {
        "id": 25,
        "dev_id": "20a83b7bde777edc2e95a2ee4a4d388a",
        "cpu_ser": "ce92b70cbc81081c",
        "emmc_ser": "0x3a956f84",
        "mac_add": "60:48:9c:41:c6:28",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141471.5860436
    },
    {
        "id": 26,
        "dev_id": "d85e4cf5a604c94b1aabb5a81bc213b7",
        "cpu_ser": "48467b588a12fdf7",
        "emmc_ser": "0x39d5cf43",
        "mac_add": "60:48:9c:41:c5:06",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141555.9945803
    },
    {
        "id": 27,
        "dev_id": "f2abb0506dd1b2abfe90a685d379ab72",
        "cpu_ser": "336690ea6ac714e9",
        "emmc_ser": "",
        "mac_add": "60:48:9c:41:c5:16",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141641.4998958
    },
    {
        "id": 28,
        "dev_id": "1862ba705debdd3020d833f1f1d14343",
        "cpu_ser": "367e9662258859a6",
        "emmc_ser": "",
        "mac_add": "60:48:9c:41:d5:d6",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141753.2224603
    },
    {
        "id": 29,
        "dev_id": "4ce7840d0731559f7fcbfed124692524",
        "cpu_ser": "317a6358df8764c5",
        "emmc_ser": "0x3ad5bff0",
        "mac_add": "60:48:9c:41:c6:12",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141933.1439383
    },
    {
        "id": 30,
        "dev_id": "f6e91868247b4b1322beaca2ef8ce59e",
        "cpu_ser": "e71c57cc468958bb",
        "emmc_ser": "0x39d5cf26",
        "mac_add": "60:48:9c:41:c5:ca",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759141956.5712852
    },
    {
        "id": 31,
        "dev_id": "5110ab475649ee9552358f5a1a50d522",
        "cpu_ser": "5ba9b780bbeccc05",
        "emmc_ser": "0x39d5cf2c",
        "mac_add": "60:48:9c:41:c6:32",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142052.010942
    },
    {
        "id": 32,
        "dev_id": "3ec10974830c73460f54e467591b0f6a",
        "cpu_ser": "aa6fc1506ac714e9",
        "emmc_ser": "0x3a55f3ca",
        "mac_add": "60:48:9c:41:c4:f4",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142204.6201627
    },
    {
        "id": 33,
        "dev_id": "310989716fb404c64ec06daf82007b44",
        "cpu_ser": "971f142d4f387954",
        "emmc_ser": "0x3a156eac",
        "mac_add": "60:48:9c:41:c5:fa",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142287.2134218
    },
    {
        "id": 34,
        "dev_id": "744a465c12c9f2ecacca772c158ad7ff",
        "cpu_ser": "0cd40efaafeafd8e",
        "emmc_ser": "0x3a156f18",
        "mac_add": "60:48:9c:41:d5:ec",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142362.8570936
    },
    {
        "id": 35,
        "dev_id": "2002f7d17ceaf4588d5872b15e2fc7e6",
        "cpu_ser": "81614ffa9e13a1b8",
        "emmc_ser": "0x3a55f3af",
        "mac_add": "60:48:9c:41:c6:0a",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142447.4260025
    },
    {
        "id": 36,
        "dev_id": "9c3f996b71f6c4dec658f5ff5c4cc5da",
        "cpu_ser": "872e8c87a57d8170",
        "emmc_ser": "0x3a55f399",
        "mac_add": "60:48:9c:41:c5:ea",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142512.577798
    },
    {
        "id": 37,
        "dev_id": "cf028e34aad4e59e982cf59ec85b72f6",
        "cpu_ser": "8007f4445c54e102",
        "emmc_ser": "0x3ad5bff9",
        "mac_add": "60:48:9c:41:d5:ce",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142618.3261442
    },
    {
        "id": 38,
        "dev_id": "534a1f56d5c1ab6efa23957c8b03a4fc",
        "cpu_ser": "9ac8aa2bbbeccc05",
        "emmc_ser": "0x3a957021",
        "mac_add": "60:48:9c:41:d5:fc",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759142681.0594652
    },
    {
        "id": 39,
        "dev_id": "f6611e23392d1741f8cd180caf8d70f5",
        "cpu_ser": "8a969a7fbc81081c",
        "emmc_ser": "",
        "mac_add": "60:48:9c:41:d6:1c",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759143317.7505105
    },
    {
        "id": 41,
        "dev_id": "6948ff178f66fb9006cdff1f6290c1ec",
        "cpu_ser": "d5958baaaba60942",
        "emmc_ser": "0x3ad5c002",
        "mac_add": "60:48:9c:41:c4:f6",
        "firmware_version": "20250929",
        "app_version": "",
        "last_active": 1759145230.4834993
    }
]


def register_device(device_data):
    """注册单个设备"""
    # 准备请求数据
    request_data = {
        "dev_id": device_data["dev_id"],
        "cpu_ser": device_data["cpu_ser"],
        "emmc_ser": device_data["emmc_ser"],
        "mac_add": device_data["mac_add"],
        "firmware_version": device_data["firmware_version"],
        "app_version": device_data["app_version"],
        "request": "factory_activate",
        "event_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }

    try:
        # 发送注册请求
        response = requests.post(
            f"{BASE_URL}/device/factory_activated",
            json=request_data,
            headers={"Content-Type": "application/json"}
        )

        if response.status_code == 200:
            # 获取纯文本响应（不带引号）
            device_name = response.text.strip()
            print(f"设备 {device_data['dev_id']} 注册成功: {device_name}")
            return device_name
        else:
            print(
                f"设备 {device_data['dev_id']} 注册失败: HTTP {response.status_code} - {response.text}")
            return None
    except Exception as e:
        print(f"设备 {device_data['dev_id']} 注册异常: {str(e)}")
        return None


def main():
    """主函数"""
    print(f"开始重新注册 {len(devices)} 台设备")

    successful_registrations = 0
    new_devices = {}

    for i, device in enumerate(devices, start=1):
        # 注册设备
        device_name = register_device(device)

        if device_name:
            successful_registrations += 1
            new_devices[device["dev_id"]] = device_name
            # 每注册1台设备打印一次进度
            print(f"已注册 {successful_registrations} 台设备")

        # 避免请求过于频繁，可以适当休眠
        time.sleep(0.1)

    print(f"设备重新注册完成，成功注册 {successful_registrations}/{len(devices)} 台设备")

    # 保存新注册的设备信息（可选）
    if new_devices:
        with open("re_registered_devices.json", "w") as f:
            json.dump(new_devices, f, indent=2)
        print("已保存重新注册的设备信息到文件")


if __name__ == "__main__":
    main()